package com.wt.springsamples.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.springsamples.biz.mybatis.mapper.SysMenuFuncRefMapper;
import com.wt.springsamples.biz.mybatis.mapper.SysMenuMapper;
import com.wt.springsamples.biz.service.ISysConfigService;
import com.wt.springsamples.common.BizException;
import com.wt.springsamples.model.Org;
import com.wt.springsamples.model.SysFunction;
import com.wt.springsamples.model.SysMenu;
import com.wt.springsamples.model.SysMenuFuncRef;
import com.wt.springsamples.model.dto.MenuFuncConfigDto;
import com.wt.springsamples.model.dto.MenuTreeNode;
import com.wt.springsamples.model.dto.OrgTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysConfigServiceImpl implements ISysConfigService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysMenuFuncRefMapper sysMenuFuncRefMapper;


    @Override
    public void createMenu(SysMenu sysMenu) {
        if(sysMenu.getPid()==null){
            sysMenu.setPid(0L);
        }
        sysMenuMapper.insert(sysMenu);
    }

    @Override
    public void updateMenu(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void deleteMenuById(long menuId) {
        sysMenuMapper.deleteById(menuId);
    }

    @Override
    public void deleteMenuByIds(List<Long> idList) {
        sysMenuMapper.deleteBatchIds(idList);
    }

    @Override
    public List<SysMenu> listAllMenus() {
        return sysMenuMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public MenuTreeNode getMenuTreeNodeInfo(long menuId) {
        List<SysMenu> allList = sysMenuMapper.selectList(new QueryWrapper<>());
        SysMenu sysMenu = findSysMenuById(allList,menuId);
        if(sysMenu == null){
            throw new BizException(50001,"数据不存在");
        }
        List<MenuTreeNode> children = list2Tree(convertListBy(allList),sysMenu.getId());
        MenuTreeNode node = new MenuTreeNode();
        node.setId(sysMenu.getId());
        node.setName(sysMenu.getName());
        node.setPid(sysMenu.getPid());
        node.setChildren(children);
        return node;
    }



    @Override
    public List<MenuTreeNode> getMenuTreeNodeList() {
        List<SysMenu> allList = sysMenuMapper.selectList(new QueryWrapper<>());
        List<MenuTreeNode>  treeNodeList = convertListBy(allList);
        return list2Tree(treeNodeList,0);
    }


    private List<MenuTreeNode> convertListBy(List<SysMenu> sourceList){
        List<MenuTreeNode>  list = sourceList.stream().map((o)->{
            MenuTreeNode treeNode = new MenuTreeNode();
            treeNode.setId(o.getId());
            treeNode.setName(o.getName());
            treeNode.setPid(o.getPid());
            return treeNode;
        }).collect(Collectors.toList());
        return list;
    }

    private List<MenuTreeNode> list2Tree(List<MenuTreeNode> nodeList, long pid){
        List<MenuTreeNode> children = new ArrayList<>();
        for(MenuTreeNode node:nodeList){
            if(node.getPid() == pid){
                node.setChildren(list2Tree(nodeList,node.getId()));
                children.add(node);
            }
        }
        return  children;
    }

    private  SysMenu findSysMenuById(List<SysMenu> list,long id){
        for(SysMenu item:list){
            if(id==item.getId()){
                return item;
            }
        }
        return null;
    }

    private List<SysMenu> findSysMenuListByPid(List<SysMenu> list,long pid){
        List<SysMenu> children = new ArrayList<>();
        for(SysMenu item:list){
            if(pid==item.getPid()){
                children.add(item);
            }
        }
        return children;
    }

    @Override
    public void configMenuApi(MenuFuncConfigDto menuFuncConfigDto) {
        //删除原有配置
        QueryWrapper<SysMenuFuncRef>  queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuFuncConfigDto.getMenuId());
        sysMenuFuncRefMapper.delete(queryWrapper);
        //新增
        List<Long> funcIdList = menuFuncConfigDto.getFuncIdList();
        List<SysMenuFuncRef> refList = new ArrayList<>(funcIdList.size());
        for(Long funcId:funcIdList){
            SysMenuFuncRef ref = new SysMenuFuncRef();
            ref.setMenuId(menuFuncConfigDto.getMenuId());
            ref.setFuncId(funcId);
            refList.add(ref);
            sysMenuFuncRefMapper.insert(ref);
        }
    }

    @Override
    public List<SysFunction> listApiOfMenu(long menuId) {
        SysMenu menuDbObj = sysMenuMapper.selectById(menuId);
        if(menuDbObj==null){
            throw  new BizException(50001,"记录不存在");
        }
        return sysMenuMapper.listApiOfMenu(menuId);
    }

    @Override
    public void addMenuApi(Long menuId, Long funcId) {
        SysMenuFuncRef ref = new SysMenuFuncRef();
        ref.setMenuId(menuId);
        ref.setFuncId(funcId);
        sysMenuFuncRefMapper.insert(ref);
    }

    @Override
    public void removeMenuApi(Long menuId, Long funcId) {
        QueryWrapper<SysMenuFuncRef>  queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId);
        queryWrapper.eq("func_id",funcId);
        sysMenuFuncRefMapper.delete(queryWrapper);
    }
}
